import { defineStore } from 'pinia'
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import * as commentApi from '@/api/comment'
import type { Comment, CommentData } from '@/api/comment'

export const useCommentStore = defineStore('comment', () => {
  const comments = ref<Comment[]>([])
  const loading = ref(false)

  // 获取帖子评论列表
  const fetchComments = async (postId: number) => {
    try {
      loading.value = true
      const res = await commentApi.getPostComments(postId)
      if (res.code === 200) {
        comments.value = res.data
      }
    } catch (error: any) {
      message.error(error.message || '获取评论列表失败')
    } finally {
      loading.value = false
    }
  }

  // 创建评论
  const createComment = async (data: CommentData): Promise<boolean> => {
    try {
      loading.value = true
      const res = await commentApi.createComment(data)
      if (res.code === 200) {
        // 如果是回复评论，将新评论添加到父评论的children中
        if (data.parentId) {
          const parentComment = findCommentById(comments.value, data.parentId)
          if (parentComment) {
            if (!parentComment.children) {
              parentComment.children = []
            }
            parentComment.children.push(res.data)
          }
        } else {
          // 如果是根评论，直接添加到列表中
          comments.value.unshift(res.data)
        }
        message.success('评论成功')
        return true
      }
      return false
    } catch (error: any) {
      message.error(error.message || '评论失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 点赞评论
  const likeComment = async (id: number): Promise<boolean> => {
    try {
      const res = await commentApi.likeComment(id)
      if (res.code === 200) {
        // 更新评论的点赞数
        const comment = findCommentById(comments.value, id)
        if (comment) {
          comment.likes++
        }
        return true
      }
      return false
    } catch (error: any) {
      message.error(error.message || '点赞失败')
      return false
    }
  }

  // 删除评论
  const deleteComment = async (id: number): Promise<boolean> => {
    try {
      loading.value = true
      const res = await commentApi.deleteComment(id)
      if (res.code === 200) {
        // 从评论列表中删除评论
        removeCommentById(comments.value, id)
        message.success('删除成功')
        return true
      }
      return false
    } catch (error: any) {
      message.error(error.message || '删除失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 递归查找评论
  const findCommentById = (commentList: Comment[], id: number): Comment | null => {
    for (const comment of commentList) {
      if (comment.id === id) {
        return comment
      }
      if (comment.children) {
        const found = findCommentById(comment.children, id)
        if (found) {
          return found
        }
      }
    }
    return null
  }

  // 递归删除评论
  const removeCommentById = (commentList: Comment[], id: number): void => {
    const index = commentList.findIndex(comment => comment.id === id)
    if (index > -1) {
      commentList.splice(index, 1)
      return
    }
    for (const comment of commentList) {
      if (comment.children) {
        removeCommentById(comment.children, id)
      }
    }
  }

  return {
    comments,
    loading,
    fetchComments,
    createComment,
    likeComment,
    deleteComment
  }
}) 